<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#localSelect").change(function(){
			$(':text').attr('readonly', false);
			
			var selectLoc_no=$(this).val();
			if(selectLoc_no==""){
				return;
			}
			var selectLoc =$("#selectLocList option[value="+selectLoc_no+"]").text();
			
			$.ajax({
				type:"POST",
	      	   	url:"admin.do",
	      	   	data:"command=getTheaterListByLocalNo&loc_no="+selectLoc_no,
	      	   	dataType:"json",
	      	   	success:function(json){
		      	   	var select="<form id='selectTheaterList'><select id='theaterSelect' name='theater_no'><option value=defulat>영화관 선택</option>";
					  $.each(json.list,function(index,value){
					      select+=" <option value="+value.THEATER_NO+">"+value.THEATER+"</option>";
					     });
						select+="</form></select>";		
					$("#theaterList").html(select);
					$("#inputLocName").html(selectLoc);
					$("#l_no").val(selectLoc_no);
	      	   	}				
			});//ajax
		});//loc select
		$("#theaterList").on("change","#theaterSelect",function(){
			$(':text').attr('readonly', false);
			var theater_no = $(this).val();
			if(theater_no==""){
				return;
			}
			alert("셀렉트한 영화관 번호 : "+theater_no);
			$("#hidden_tno").val(theater_no);//상영관 등록시 영화관 고유 번호가 필요함으로 hidden 에 저장
			var theater=$("#selectTheaterList option[value="+theater_no+"]").text();
			$.ajax({
				type:"POST",
				url:"admin.do",
				data:"command=getTheaterInfoByTNO&theater_no="+theater_no//영화관 번호 param
				+"&localInfoVO.loc="+$("#inputLocName").text()//지역명 
				+"&localInfoVO.loc_no="+$("#l_no").val(),//지역번호
				dataType:"json",
	      	   	success:function(json){
	      	   		$("#t_no").val(json.tvo.theater_no);
	      	   		$("#modifyAdd").val(json.tvo.theater_addr);
	      	   		$("#modifyTel").val(json.tvo.theater_tel);
	      	   	}
			});
			$("#inputTheaterName").val(theater);
		});//local change
		
		//영화관 수정버튼 클릭 메서드
		$("#modifyTheaterInfo").click(function(){
			var theater_no=$("#t_no").val();
			var theater=$("#inputTheaterName").val();
			var loc_no=$("#l_no").val();
			var loc=$("#inputLocName").text();
			var theater_addr=$("#modifyAdd").val();
			var theater_tel=$("#modifyTel").val();
			if(theater_no==""||theater==""||loc_no==""||loc==""||theater_addr==""||theater_tel==""){
				alert("공란 없이 입력해주세요.--^");
			}
			$.ajax({
				type:"POST",
				url:"admin.do",
				data:"command=theater_modify&theater_no="+theater_no+
				"&theater="+theater+"&localInfoVO.loc_no="+loc_no+"&localInfo.loc="+loc+
				"&theater_addr="+theater_addr+"&theater_tel="+theater_tel,
				dataType:"json",
				success:function(json){
					$(':text').attr('readonly', true);
					//새롭게 등록가능한 상영관 번호를 출력 
					var prNoList="<select id='selectRegPrNo'>";
						for(i=0;i<json.map.prNoList.length;i++){
							var prno=json.map.prNoList[i];
							prNoList+="<option value='"+prno+"'>";
							prNoList+=prno+"</option>";
						}
					prNoList+="</select>";
					
					
					//등록된 상영관 정보를 체크박스로 출력
						var radio="";
						for(i=0;i<json.map.prList.length;i++){
							var m=json.map.prList[i].play_room_no;
							radio+="<tr><td colspan='5' align='center'><b>"+m+"</b> 번관 &nbsp;&nbsp; 좌석수 : "+json.map.prList[i].seat_capacity;
							radio+="&nbsp;&nbsp;&nbsp;<input type='checkbox' name='delPlayRoomCheck' value='"+m+"'>"
							+"<input class='uniq' type='hidden' name='"+m+"' value='"+json.map.prList[i].play_room_uniq_no+"'></td></tr>";
						}
					$("#modifyPlayRoomZone").html("<table  width=''  cellspacing='0' border='1' >"
							+"<tr><td width='500' colspan='3' align='center'><b>신규 상영관 등록</b>&nbsp;&nbsp;&nbsp;<font color='darkred'>: 최대 20관까지 등록 가능</font></td></tr>"
							+"<tr  align='center'><td width='70'>"+prNoList+"</td><td width='70'>"+selectPrTypeList(json.map.prTypeList)+"</td><td width='60'>"
							+"<input type='button' id='inputPlayRoomInfoBtn' value='등록'></td>"
							+"</tr><tr align='center'><td colspan='2' ><b>기존 상영관 삭제</b>&nbsp;&nbsp;&nbsp;<font color='darkred'>: 예매 정보가 있는 상영관은 삭제 불가</font></td><td><span id='deleteRegedPlayRoomBtnZone'></td></tr>"
							+radio+"</table>"		
					);//html
					$("#deleteRegedPlayRoomBtnZone").html(
							"<input type='button' id='deleteRegedPlayRoomBtn' value='상영관 삭제'>"
					);//html
					$("#insertPlayRoomTypeImageZone").html("<br><b>선택타입 : a 타입</b><br><img width='450' src='img.playroom_type/"+$("#selectRegPrType").val()+"_type.jpg'>");
				}//success
			});//ajax
		});//영화관 수정하기 button
		
		//상영관 등록
		$("#modifyPlayRoomZone").on("click","#inputPlayRoomInfoBtn",function(){
			var tno=$("#hidden_tno").val();
			var pno=$("#selectRegPrNo").val();
			var ptype=$("#selectRegPrType").val();
			var pseat=$(":input:hidden:[name='"+ptype+"']").val();
			alert(pseat+" "+ptype);
			$.ajax({
				type:"POST",
				url:"admin.do",
				data:"command=playRoom_reg&play_room_no="+pno
				+"&seat_capacity="+pseat
				+"&theaterVO.theater_no="+tno
				+"&play_room_type_id="+ptype,
				dataType:"json",
				success:function(json){
					alert("성공");
				} 
			});//ajax
			  $('#modifyTheaterInfo').trigger("click");
		});//상영관 등록
		
		//상영관 삭제
		$("#modifyPlayRoomZone").on("click","#deleteRegedPlayRoomBtn",function(){
			var items=[];
			var prNo=[];
			var items_str;
			  $("input[name='delPlayRoomCheck']:checkbox:checked").each(function(){
				  items.push($(this).val());
			  });
			  for(i=0;i<items.length;i++){
				var d=$(":input:hidden:[name='"+items[i]+"']").val();
				prNo.push(d);
			  }
			  items_str= items.join(',');
			  if(confirm ("["+items_str+'] 번 관을 삭제하시겠습니까?')){
				  	$.ajax({
					type:"POST",
					url:"admin.do",
					data:"command=playRoom_del&delList="+prNo,
					dataType:"json",
					success:function(json){
						var message="결과  : ";
						var j=json.delResult;
						if(j.success!=null){
							message+="["+j.success+"]관 삭제 성공 ";
						}
						if(j.fal!=null){
							message+=" ["+j.fal+"]관 삭제 실패 : 해당 상영관에 예매정보가 존재합니다.";
						}
						alert(message);
					}//success
				  });//ajax
			  }//confirm
				  $('#modifyTheaterInfo').trigger("click");
		});//상영관 삭제
		
		//등록 가능한 상영관 타입 이미지 출력
		$("#modifyPlayRoomZone").on("change","#selectRegPrType",function(){
			$("#insertPlayRoomTypeImageZone").html("<img src='img.playroom_type/"+$(this).val()+"_type.jpg'>");
		});//on
		
	});
	
	//등록가능한 상영관 타입 리스트 로딩
	function selectPrTypeList(list){
		var prNoList="<select id='selectRegPrType'>";
			for(i=0;i<list.length;i++){
				var prId=list[i].play_room_type_id;
				var prCap=list[i].play_room_capacity;
				prNoList+="<option value='"+prId+"'>";
				prNoList+=prId+"타입  :  상영관 수"+prCap+"석</option>";
				prNoList+="<input type='hidden' name='"+prId+"'value='"+prCap+"'>";
			}
		prNoList+="</select>";
		return prNoList;
	}
</script>
<c:choose>
<c:when test="${sessionScope.vo.memberlevelinfoVO.member_level_no !=1}">
<script>
	alert("잘못된 접근입니다!");           
	location.href="mycinema.do?command=home_main";
</script>
</c:when>
</c:choose>
<!-- 상영관 등록시 필요한 정보를 hidden 값에 저장해둠 -->
<input type="hidden" id="hidden_tno" value="">
<input type="hidden" id="hidden_prno" value="">
<input type="hidden" id="hidden_prseat" value="">
<font class="title">영화관 정보 수정</font>
<hr color="#f5f5f5">
<font size="3" face="고딕" color="red"><b>지역과 영화관을 선택해 주세요.</b></font>
<br>
<br>
<table>
	<tr>
		<td><input type="hidden" id="l_no" value=""> <input
			type="hidden" id="t_no" value="">
			<table cellspacing='0' border='1' height='150px'>
			<tr><td  width='500' colspan='4' align="center"><b>영화관을 선택해 주세요.</b></tr>
				<tr align="center">
					<td colspan="2" width="200">지 역</td>
					<td colspan="2" width="200">
						<form id="selectLocList">
							<select id="localSelect">
								<option value="">- 지역 선택 -</option>
								<c:forEach items="${loc_list}" var="local">
									<option value="${local.loc_no}">${local.loc}</option>
								</c:forEach>
							</select>
						</form>
					</td>
				</tr>
				<tr align="center">
					<td colspan="2">영화관</td>
					<td colspan="2"><span id='theaterList'></span></td>
				</tr>
				<tr>
					<td width="20%" align="center">지역명</td>
					<td width="30%" align="center"><span id="inputLocName"></span></td>
					<td width="20%" align="center">영화관 명</td>
					<td width="30%" align="center"><input type="text"
						id="inputTheaterName" value=""></td>
				</tr>
				<tr>
					<td align="center">주소</td>
					<td align="left" colspan="3"><input size="50" type="text"
						id="modifyAdd" value=""></td>
				</tr>
				<tr>
					<td align="center">연락처</td>
					<td align="left" colspan="2"><input size="24" type="text"
						id="modifyTel" value=""></td>
					<td align="center"><input type="button" id="modifyTheaterInfo"
						value="영화관 정보 수정"></td>
				</tr>
			</table></td>
</tr><tr>
	<td><br><br><span id="modifyPlayRoomZone"> </span></td></tr>
		<tr>
			<td align='center'><div id="insertPlayRoomTypeImageZone"></div></td>
	</tr>
</table>
